//
//  MessageTextView.m
//  gunrenjiaoyu
//
//  Created by 张艳江 on 17/5/4.
//  Copyright © 2017年 张艳江. All rights reserved.
//

#import "MessageTextView.h"

#define getLimitNum (self.limitNum > 0 ? self.limitNum : 170)

@interface MessageTextView()

@property (assign, nonatomic) BOOL isLimit;//是否限制字数

@end

@implementation MessageTextView

- (UILabel *)countNumLabel{
    
    if (!_countNumLabel) {
        _countNumLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width - 90, self.frame.size.height - 21, 80, 21)];
        _countNumLabel.textAlignment = NSTextAlignmentRight;
        _countNumLabel.font = SCFont(14);
        _countNumLabel.textColor = [UIColor grayColor];
        _countNumLabel.backgroundColor = [UIColor clearColor];
    }
    return _countNumLabel;
}

- (id)initWithFrame:(CGRect)frame text:(NSString *)text isLimit:(BOOL)isLimit{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.font = SCFont(14);
        self.textColor = [UIColor blackColor];
        self.isLimit = isLimit;
        if (self.isLimit == YES) {
            self.returnKeyType = UIReturnKeyDone;
            [self addSubview:self.countNumLabel];
        }
        self.delegate = self;
        
        self.placeholderLabel = [[UILabel alloc]initWithFrame:CGRectMake(8, 8, self.bounds.size.width - 16, 0)];
        self.placeholderLabel.font = SCFont(14);
        NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:3];
        NSAttributedString *string = [[NSAttributedString alloc] initWithString:text attributes:@{NSParagraphStyleAttributeName: paragraphStyle}];
        self.placeholderLabel.attributedText = string;
        self.placeholderLabel.textColor = [UIColor lightGrayColor];
        self.placeholderLabel.backgroundColor = [UIColor clearColor];
        self.placeholderLabel.numberOfLines = 0;
        [self.placeholderLabel sizeToFit];
        [self addSubview:self.placeholderLabel];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChanged:) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}
- (void)layoutSubviews{
    
    self.countNumLabel.text = [NSString stringWithFormat:@"%lu/%ld",(unsigned long)self.text.length, getLimitNum];
}
- (void)textChanged:(NSNotification *)notification{
    
    NSInteger wordCount = self.text.length;
    if (self.isLimit == YES) {
        if (wordCount >= getLimitNum) {
            
            NSString *rangeStr = [self.text substringToIndex:getLimitNum];
            self.text = rangeStr;
            self.countNumLabel.text = [NSString stringWithFormat:@"%ld/%ld",getLimitNum,getLimitNum];
            [self endEditing:YES];
            return;
        }else{
            self.countNumLabel.text = [NSString stringWithFormat:@"%ld/%ld",(long)wordCount,getLimitNum];
        }
    }
    if (self.text.length == 0) {
        self.placeholderLabel.hidden = NO;
    }else{
        self.placeholderLabel.hidden = YES;
    }
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text{
    
    if (self.isLimit == YES) {
        if ([text isEqualToString:@"\n"]) {
            [textView resignFirstResponder];
            return NO;
        }
    }
    return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    //NSLog(@"最终输入的文字：%@",textView.text);
    if (self.endText) {
        self.endText(textView.text);
    }
    return YES;
}
-(void) dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
